package com.cauli.manage.notice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cauli.manage.notice.model.dto.NoticeAddDTO;
import com.cauli.manage.notice.model.vo.NoticeVO;
import com.cauli.manage.notice.model.dto.NoticeQueryDTO;
import com.cauli.manage.notice.model.dto.NoticeUpdateDTO;
import com.cauli.manage.notice.model.dto.NoticeUpdateStatusDTO;
import com.cauli.manage.notice.model.entity.Notice;
import com.cauli.manage.notice.service.NoticeService;
import com.cauli.utils.util.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Cauli
 * @date 2023-03-18 12:00
 * @description 公告管理 前端控制器
 */
@Api(tags = "公告管理")
@ApiSort(3)
@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取公告列表")
    @GetMapping("/list")
    public ResultResponse<Notice> listNotice(NoticeQueryDTO noticeQueryDTO) {
        Page<Notice> noticePage = noticeService.getNoticePage(noticeQueryDTO);
        return ResultResponse.success().dataList(noticePage.getRecords(), noticePage.getTotal());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("根据公告ID获取详细信息")
    @GetMapping("/{noticeId}")
    public ResultResponse<NoticeVO> getNotice(@PathVariable(value = "noticeId") Long noticeId) {
        NoticeVO noticeVO = noticeService.getNoticeById(noticeId);
        return ResultResponse.success().data(noticeVO);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("新增公告")
    @PostMapping
    public ResultResponse<Void> addNotice(@Valid @RequestBody NoticeAddDTO noticeAddDTO) {
        noticeService.addNotice(noticeAddDTO);
        return ResultResponse.success().message("添加成功");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("修改公告")
    @PutMapping
    public ResultResponse<Void> updateNotice(@Valid @RequestBody NoticeUpdateDTO noticeUpdateDTO) {
        noticeService.updateNotice(noticeUpdateDTO);
        return ResultResponse.success().message("修改成功");
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("公告状修改态")
    @PutMapping("/status")
    public ResultResponse<Void> changeNoticeStatus(@Valid @RequestBody NoticeUpdateStatusDTO noticeUpdateStatusDTO) {
        noticeService.changeNoticeStatus(noticeUpdateStatusDTO);
        return ResultResponse.success().message("修改成功");
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("删除公告")
    @DeleteMapping( "/{noticeIds}")
    public ResultResponse<Void> deleteNotice(@PathVariable List<Long> noticeIds) {
        noticeService.deleteNotice(noticeIds);
        return ResultResponse.success().message("删除成功");
    }
}
